package Listening;

import View.Login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class Enroll {
    JFrame jf = new JFrame("修改密码");


    JTextField ut = new JTextField(15);
    JPasswordField pt = new JPasswordField(15);
    ButtonGroup bg = new ButtonGroup();
    JLabel xj = new JLabel("性        别");
    JRadioButton nan = new JRadioButton("男", true);
    JRadioButton nv = new JRadioButton("女", true);

    JPasswordField ppt = new JPasswordField(15);
    JLabel ts = new JLabel();

    public void into() {
//        jf.setSize(500, 310);

        jf.setResizable(false);

        ImageIcon bbg = new ImageIcon("./souse/images/a.gif");
        JLabel label = new JLabel(bbg);
        label.setSize(bbg.getIconWidth(), bbg.getIconHeight());
        jf.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
//        2.把窗口面板设为内容面板并设为透明、流动布局。
        JPanel pan = (JPanel) jf.getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new FlowLayout());

        Box b = Box.createVerticalBox();

        Box ub = Box.createHorizontalBox();
        JLabel uj = new JLabel("学        号");
        uj.setForeground(Color.WHITE);

        ub.add(uj);
        ub.add(Box.createHorizontalStrut(20));
        ub.add(ut);

        Box pb = Box.createHorizontalBox();
        JLabel pj = new JLabel("原  密  码");
        pj.setForeground(Color.white);

        pb.add(pj);
        pb.add(Box.createHorizontalStrut(20));
        pb.add(pt);

        Box ppb = Box.createHorizontalBox();
        JLabel ppj = new JLabel("修改密码");
        ppj.setForeground(Color.red);

        ppb.add(ppj);
        ppb.add(Box.createHorizontalStrut(20));
        ppb.add(ppt);

        Box xb = Box.createHorizontalBox();
        JLabel wj=new JLabel("注：若初始密码忘记请与管理员联系");
        wj.setForeground(Color.yellow);

        bg.add(nan);
        bg.add(nv);
        xb.add(wj);
//        xb.add(Box.createHorizontalStrut(20));
//        xb.add(nan);
//        xb.add(nv);
//        xb.add(Box.createHorizontalStrut(120));

        Box box = Box.createHorizontalBox();
        JButton zhuce = new JButton("修改");
        JButton fanhui = new JButton("返回登陆页面");
        box.add(fanhui);
        box.add(Box.createHorizontalStrut(80));
        box.add(zhuce);

        Box ts1 = Box.createHorizontalBox();
        ts1.add(ts);

        b.add(Box.createVerticalStrut(9));
        b.add(ub);
        b.add(Box.createVerticalStrut(30));
        b.add(pb);
        b.add(Box.createVerticalStrut(30));
        b.add(ppb);
        b.add(Box.createVerticalStrut(30));
        b.add(xb);
        b.add(Box.createVerticalStrut(30));
        b.add(box);
        b.add(ts1);
        jf.setSize(bbg.getIconWidth(), bbg.getIconHeight());
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jf.add(b);
        jf.setLayout(new FlowLayout());

        fanhui.addActionListener(new fanhuiJT());
        zhuce.addActionListener(new zhuceJT());
        jf.setVisible(true);
    }

//    public static void main(String[] args) {
//        new zhuce().into();
//    }

    public class fanhuiJT implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            new Login().zb();
            jf.dispose();
        }
    }

    public class zhuceJT implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            ts.setForeground(Color.red);
//用户名   判断是否为空     判断用户名是否存在   插入数据库    返回登陆页面
//mima
//xingbie

            String zhanghao = ut.getText().trim();
//            System.out.println(zhanghao);
            String mima = pt.getText().trim();
            String queren = ppt.getText().trim();
            String xingbie = bg.isSelected(nan.getModel()) ? nan.getText() : nv.getText();

            if (zhanghao.equals("") || mima.equals("") || queren.equals("")) {
                ts.setText("某项信息不能为空");
            } else {

                    try {
                        Class.forName("com.mysql.cj.jdbc.Driver");
                        Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/lxt", "root", "111111");
                        Statement s = c.createStatement();
                        ResultSet result = s.executeQuery("select *from evaluate where 学号='" + zhanghao + "'");
                        if (result.next()) {
                                ResultSet resultSet=s.executeQuery("select *from evaluate where 密码='" + mima + "'");
                                if (resultSet.next()){
                                    s.executeUpdate("update  evaluate set 密码='"+queren+"' where 学号 ='"+zhanghao+"'");
                                    ts.setText("密码修改成功");
                                }else {
                                    ts.setText("原密码错误");
                                }
                        } else {

                            ts.setText("此学号不存在");


                        }
                        result.close();
                        s.close();
                        c.close();
                    } catch (ClassNotFoundException a) {
                        a.printStackTrace();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }



            }
        }
    }

    public static void main(String[] args) {
        new Enroll().into();
    }
}